package video.utils;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
@Component
public class BlockExcept implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
        System.out.println("BlockExceptionHandler----");
        HashMap<String,Object> map = new HashMap<>();
        if(e instanceof FlowException){
            map.put("code",100);
            map.put("msg",11+"TT");
        } else if (e instanceof DegradeException) {
             map.put("code",101);
             map.put("msg",22+"TT");
        } else if (e instanceof ParamFlowException) {
            map.put("code",102);
            map.put("msg",33+"TT");
            
        } else if (e instanceof SystemBlockException) {
            map.put("code",103);
            map.put("msg",44+"TT");
        } else if (e instanceof AuthorityException) {
            map.put("code",104);
            map.put("msg",55+"TT");
        }
//        else if(e instanceof RuntimeException){
//            map.put("code",105);
//            map.put("msg",66+"TT");
//        }
        httpServletResponse.setStatus(200);
        httpServletResponse.setHeader("content-type","application/json;charset=UTF-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(map));


    }
}
